//
//  FindTableViewCell.m
//  A Learning Walk
//
//  Created by 张勇 on 16/6/14.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "FindTableViewCell.h"
#import "FindCollectionViewCell.h"

@interface FindTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,retain)UICollectionView *cllectionView;
@property(nonatomic,retain)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,retain)NSMutableArray *sectionArray;
@property(nonatomic,retain)NSMutableArray *myArray;

@end


@implementation FindTableViewCell

-(void)dealloc{
    
    [_cllectionView release];
    [_flowLayout release];
    [_myArray release];
    [_sectionArray release];
    [super dealloc];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myArray = [NSMutableArray array];
        self.sectionArray = [NSMutableArray array];
        [self CreateSubViews];
        
    }
    return self;
}

-(void)CreateSubViews{

   self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //self.flowLayout.itemSize = CGSizeMake(80, 100);
   // self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.cllectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 20)/ 3, self.frame.size.height - 10) collectionViewLayout:self.flowLayout];
    self.cllectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.cllectionView];
    self.cllectionView.delegate = self;
    self.cllectionView.dataSource = self;
    
    [_flowLayout release];
    [_cllectionView release];
    //注册
    [self.cllectionView registerClass:[FindCollectionViewCell class] forCellWithReuseIdentifier:@"fingReuse"];
    //滚动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    [self.cllectionView reloadData];

}


//重新布局
-(void)layoutSubviews{
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake((self.frame.size.width - 40) / 3, self.frame.size.height - 20);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    self.cllectionView.frame = CGRectMake(0, 0, self.frame.size.width , self.frame.size.height - 10);

}
//点击item跳转方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    smallListModel *smallModel = [_model.list objectAtIndex:indexPath.item];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ToDetail" object:smallModel.albumId];


}
//cell显示
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fingReuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
   
    smallListModel *smallModel = [_model.list objectAtIndex:indexPath.item];
   // NSLog(@"%@",smallModel.intro);
   [cell.imageV sd_setImageWithURL:[NSURL URLWithString:smallModel.albumCoverUrl290] placeholderImage:[UIImage imageNamed:@"holderr"]];
    cell.titleLabel.text = smallModel.intro ;
    cell.subTitleLabel.text = smallModel.nickname;
    return cell;

}

-(void)setModel:(listModel *)model{
    if (_model == model) {
        [_model release];
    }
    _model = [model retain];
    [self.cllectionView reloadData];
}
//item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    
    return _model.list.count;

}

@end
